//
//  NewFeatureCollectionVC.swift
//  我的微博
//
//  Created by gongjunna on 15/7/31.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewFeatureCollectionVC: UICollectionViewController {

    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(CollectionCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        
    }
    init() {
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLayoutSubviews() {
        layout.itemSize = (collectionView?.bounds.size)!
    }
    
    
  private   let layout : UICollectionViewFlowLayout = {
        let flow = UICollectionViewFlowLayout()
        flow.minimumInteritemSpacing = 0
        flow.minimumLineSpacing = 0
        flow.scrollDirection = UICollectionViewScrollDirection.Horizontal
        return flow
        }()
    

   
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 4
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell  = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionCell
    
        // Configure the cell
            cell.index = indexPath.item
        return cell
    }
}
class CollectionCell :UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        addcontent()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    var index : NSInteger = 0 {
        didSet {
            iconV.image = UIImage(named: "new_feature_\(index + 1)")
            btn.hidden = true
            if index == 3 {
                beginAnimation()
            }
        }
    }
    
    func  beginAnimation() {
        btn.hidden = false
        btn.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.btn.transform = CGAffineTransformIdentity
            }) { (_ ) -> Void in
                
        }
    }
    
    let  iconV = UIImageView()
    
    let btn = UIButton()
    
    func addcontent() {
       
        btn.setTitle("进入主题", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
        btn.sizeToFit()
        btn.addTarget(self, action: "loginHomeBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        contentView.addSubview(iconV)
        contentView.addSubview(btn)
        iconV.translatesAutoresizingMaskIntoConstraints = false
        btn.translatesAutoresizingMaskIntoConstraints = false
        //"V:|-0-[subview]-0-|"
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":iconV]))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":iconV]))
        contentView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: btn, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 140))
        contentView.addConstraint(NSLayoutConstraint(item: btn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
    }
    func loginHomeBtnClick() {
       // NSNotificationCenter.defaultCenter().postNotificationName(HMRootViewControllerSwitchNotification, object: true)
    
        UIApplication.sharedApplication().keyWindow?.rootViewController = MainViewController()
    }
    
}